How Do I Search For Struct Items In A Vector? [migrated]

Posted by Vladimir Marenus on Game Development See other posts from Game Development or by Vladimir Marenus
Published on 2012-11-09T18:38:09Z Indexed on 2012/11/09 23:26 UTC
Read the original article Hit count: 388

Filed under:
|
|

I'm attempting to create an inventory system using a vector implementation, but I seem to be having some troubles. I'm running into issues using a struct I made. NOTE: This isn't actually in a game code, this is a separate Solution I am using to test my knowledge of vectors and structs!

struct aItem
{
    string  itemName;
    int     damage;
};

int main()
{
    aItem healingPotion;
    healingPotion.itemName = "Healing Potion";
    healingPotion.damage= 6;

    aItem fireballPotion;
    fireballPotion.itemName = "Potion of Fiery Balls";
    fireballPotion.damage = -2;

    vector<aItem> inventory;
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(fireballPotion);

    if(find(inventory.begin(), inventory.end(), fireballPotion) != inventory.end())
                {
                        cout << "Found";
                }

    system("PAUSE");
    return 0;
}

The preceeding code gives me the following error:

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(3186): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'aItem' (or there is no acceptable conversion)

There is more to the error, if you need it please let me know. I bet it's something small and silly, but I've been thumping at it for over two hours. Thanks in advance!

© Game Development or respective owner

Related posts about c++

Related posts about vector